/*
 * FindBugs - Find Bugs in Java programs
 * Copyright (C) 2003-2008 University of Maryland
 * 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

package edu.umd.cs.findbugs.gui2;

import java.awt.CardLayout;
import java.awt.Container;
import java.awt.Cursor;

import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;


/**
 * @author Graham Allan (grallan@cis.strath.ac.uk)
 */
public class ShowHidePanel extends JPanel implements IViewController {

	static final String SHOW = "Show";
	static final String HIDE = "Hide";
	
	private final ICursorController cursorController;
	private Container waitPanel;

	public ShowHidePanel(ICursorController cursorController) {
		this.cursorController = cursorController;
		
		setLayout(new CardLayout());
		waitPanel = new JPanel();
		waitPanel.add(new JLabel("Please wait..."));
		add(waitPanel, HIDE);
	}
	
	public void setTreePanel(Container panelToShow) {
		add(panelToShow, SHOW);
	}
	
	public void hideView() {
		showCard(HIDE, new Cursor(Cursor.WAIT_CURSOR));
	}

	public void showView() {
		showCard(SHOW, new Cursor(Cursor.DEFAULT_CURSOR));
	}	
	
	private void showCard(final String cardName, final Cursor cursor) {
		if (SwingUtilities.isEventDispatchThread()) {
			cursorController.setCursor(cursor);
			CardLayout layout = (CardLayout) getLayout();
			layout.show(this, cardName);
		} else {
			final Container layoutParent = this;
			SwingUtilities.invokeLater(new Runnable() {
				public void run() {
					cursorController.setCursor(cursor);
					CardLayout layout = (CardLayout) getLayout();
					layout.show(layoutParent, cardName);
				}
			});
		}
	}

}
